<template>
<div>
<el-card shadow="never" style="min-height:650px;">
<div v-if="search_visible" id="search" class="search">
	<el-form ref="form" size="small" :model="searchData" inline>
		<el-form-item label="分类名称">
			<el-input id="class_name" v-model="searchData.class_name"  style="width:150px;" placeholder="请输入分类名称"></el-input>
		</el-form-item>
		<el-form-item label="所属分类">
			<treeselect style="width:150px" :default-expand-level="2" v-model="searchData.pid" :options="pid" :normalizer="normalizer" :show-count="true" placeholder="请选择所属分类"/>
		</el-form-item>
		<Search :search_data.sync="searchData" @refesh_list="index"></Search>
	</el-form>
</div>
<div class="btn-group" style="margin-top:10px;margin-bottom:10px;">
	<div v-if="hsz">
		<el-button size="mini" :disabled="multiple" icon="dripicons-reply" type="primary" @click="fn('restore')" >恢复数据</el-button>
	</div>
	<div v-else>
		<el-button v-for="item in button_group" v-if="checkPermission(item.access)" :key="item.access"  :disabled="$data[item.disabled]" :type="item.color" size="mini" :icon="item.icon" @click="fn(item.clickname)">
			<span v-if="item.batch" v-text="$data['batchUpdateStatus']?'批量保存':'批量编辑'"></span>
			<span v-else v-text="item.name"></span>
		</el-button>
	</div>
	<div><TableTool :search_visible.sync="search_visible" :expand_status="true" :expand="expand" @toggle="toggleRowExpansion" :hsz_status="true" :hsz.sync="hsz" @toggle_list="toggleList" @refesh_list="index"></TableTool></div>
</div>
<el-table :row-class-name="rowClass" @selection-change="selection"  @row-click="handleRowClick" :tree-props="{children: 'children'}"  :default-expand-all="expand" row-key="catagory_id"  :header-cell-style="{ background: '#eef1f6', color: '#606266' }" @sort-change='sortChange' v-loading="loading"  ref="multipleTable" border class="eltable" :data="list"  style="width: 100%">
	<el-table-column align="center" type="selection" width="42"></el-table-column>
	<el-table-column align="center" type = '' property="catagory_id"  label="编号"  width="70">
	</el-table-column>
	<el-table-column align="left"  property="class_name"  label="分类名称" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="center"  property="sortid"  label="排序号" show-overflow-tooltip width="">
		<template class="sort" slot-scope="scope">
			<el-input @blur.stop="updateExt(scope.row,'sortid')"  size="mini" placeholder="排序号" v-model="scope.row.sortid"></el-input>
		</template>
	</el-table-column>
	<el-table-column align="center"  property="pic"  label="单图上传" show-overflow-tooltip width="">
		<template slot-scope="scope">
			<div class="demo-image__preview">
				<el-image v-if="scope.row.pic" class="table_list_pic" :src="scope.row.pic"  :preview-src-list="[scope.row.pic]"></el-image>
			</div>
		</template>
	</el-table-column>
	<el-table-column :fixed="$store.getters.device == 'mobile'?false:'right'" label="操作" align="center" width="190">
		<template slot-scope="scope">
			<div v-if="scope.row.catagory_id">
				<el-button  size="mini" v-if="checkPermission('/admin/Catagory/update')" icon="el-icon-edit" type="primary" @click="update(scope.row)" >修改</el-button>
				<el-button  size="mini" v-if="checkPermission('/admin/Catagory/delete')" icon="el-icon-delete" type="danger" @click="del(scope.row)" >删除</el-button>
			</div>
		</template>
	</el-table-column>
</el-table>
<Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
</el-card>

<!--添加-->
<Add :show.sync="dialog.addDialogStatus" size="small" @refesh_list="index"></Add>
<!--修改-->
<Update :info="updateInfo" :show.sync="dialog.updateDialogStatus" size="small" @refesh_list="index"></Update>
<!--批量修改-->
<Batupdate :info="batupdateInfo" :show.sync="dialog.batupdateDialogStatus" size="small" @refesh_list="index"></Batupdate>
<!--查看详情-->
<Detail :info="detailInfo" :show.sync="dialog.detailDialogStatus" size="small" @refesh_list="index"></Detail>
<!--导入弹窗-->
<Import :show.sync="dialog.importDataDialogStatus" excel="/admin/Catagory/download" import_url='/admin/Catagory/importData' @refesh_list="index"></Import>
<!--导出弹窗-->
<el-dialog title="导出进度条" :visible="dumpshow" :before-close="closedialog" width="500px">
	<el-progress :percentage="percentage"></el-progress>
</el-dialog>

</div>
</template>

<script>
import Import from '@/components/common/Import.vue'
import Search from '@/components/common/Search.vue'
import Page from '@/components/common/Page.vue'
import TableTool from '@/components/common/TableTool.vue'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import XLSX from "xlsx"
import Add from '@/views/admin/catagory/add.vue'
import Update from '@/views/admin/catagory/update.vue'
import Batupdate from '@/views/admin/catagory/batupdate.vue'
import Detail from '@/views/admin/catagory/detail.vue'
export default {
	components: {
		Search,
		Page,
		TableTool,
		Import,
		Treeselect,
		Add,
		Update,
		Batupdate,
		Detail,
	},
	data: function() {
		return {
			dialog: {
				addDialogStatus : false,
				updateDialogStatus : false,
				batupdateDialogStatus : false,
				detailDialogStatus : false,
				importDataDialogStatus : false,
			},
			searchData:{},
			button_group:[
				{name:'添加',color:'success',access:'/admin/Catagory/add',icon:'el-icon-plus',disabled:'',clickname:'add'},
				{name:'修改',color:'primary',access:'/admin/Catagory/update',icon:'el-icon-edit',disabled:'single',clickname:'update'},
				{name:'删除',color:'danger',access:'/admin/Catagory/delete',icon:'el-icon-delete',disabled:'multiple',clickname:'del'},
				{name:'批量修改',color:'primary',access:'/admin/Catagory/batupdate',icon:'el-icon-edit',disabled:'multiple',clickname:'batupdate'},
				{name:'查看详情',color:'info',access:'/admin/Catagory/detail',icon:'el-icon-view',disabled:'single',clickname:'detail'},
				{name:'导入',color:'warning',access:'/admin/Catagory/importData',icon:'el-icon-upload',disabled:'',clickname:'importData'},
				{name:'导出',color:'warning',access:'/admin/Catagory/dumpdata',icon:'el-icon-download',disabled:'',clickname:'dumpdata'},
			],
			loading: false,
			page_data: {
				limit: 20,
				page: 1,
				total:20,
			},
			order:'',
			sort:'',
			ids: [],
			single:true,
			multiple:true,
			search_visible:true,
			expand:true,
			list: [],
			pid: [],
			updateInfo:{},
			batupdateInfo:{},
			detailInfo:{},
			exceldata:[],
			dumppage:1,
			ws:{},
			dumpshow:false,
			percentage:0,
			filename:'',
			hsz:false,
		}
	},
	methods:{
		index(){
			let param = {limit:this.page_data.limit,page:this.page_data.page,order:this.order,sort:this.sort}
			Object.assign(param, this.filteredObject(this.searchData),this.$route.query)
			this.loading = true
			this.$axios.post('/admin/Catagory/index',param).then(res => {
				if(res.data.status == 200){
					this.list = res.data.data.data
					this.page_data.total = res.data.data.total
					this.loading = false
					if(this.page_data.page == 1){
						this.pid = res.data.data.sql_field_data.pids
					}
				}else{
					this.$message.error(res.data.msg);
				}
			})
		},
		updateExt(row,field){
			if(row.catagory_id){
				this.$axios.post('/admin/Catagory/updateExt',{catagory_id:row.catagory_id,[field]:row[field]}).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}
		},
		add(){
			this.dialog.addDialogStatus = true
		},
		update(row){
			let id = row.catagory_id ? row.catagory_id : this.ids.join(',')
			this.$axios.post('/admin/Catagory/getUpdateInfo',{catagory_id:id}).then(res => {
				if(res.data.status == 200){
					this.dialog.updateDialogStatus = true
					this.updateInfo = res.data.data
				}else{
					this.$message.error(res.data.msg)
				}
			})
		},
		del(row){
			this.$confirm('确定操作吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let ids = row.catagory_id ? row.catagory_id : this.ids.join(',')
				let param = {catagory_id:ids}
				if(this.hsz){
					Object.assign(param,{onlyTrashed:true})
				}
				this.$axios.post('/admin/Catagory/delete',param).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
						this.index()
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}).catch(() => {})
		},
		batupdate(row){
			this.dialog.batupdateDialogStatus = true
			this.batupdateInfo = {catagory_id:row.catagory_id ? row.catagory_id : this.ids.join(',')}
		},
		detail(row){
			this.dialog.detailDialogStatus = true
			this.detailInfo = {catagory_id:row.catagory_id ? row.catagory_id : this.ids.join(',')}
		},
		importData(){
			this.dialog.importDataDialogStatus = true
		},
		dumpdata(){
			this.$confirm('确定操作吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.dumpshow = true
				this.confirmdumpdata()
			}).catch(() => {})
		},
		confirmdumpdata(){
			let query = {page:this.dumppage,order:this.order,sort:this.sort}
			Object.assign(query, this.searchData,{catagory_id:this.ids.join(',')},this.urlobj)
			this.$axios.post('/admin/Catagory/dumpdata',query).then(res => {
				if(res.data.data && res.data.data.length > 0){
					if(this.dumppage == 1){
						this.exceldata.push(res.data.header)
					}
					res.data.data.forEach((item) => {
						this.exceldata.push(Object.values(item))
					})
					this.percentage = res.data.percentage
					this.filename = res.data.filename
					this.ws = XLSX.utils.aoa_to_sheet(this.exceldata)
					this.dumppage = this.dumppage + 1
					this.confirmdumpdata()
				}else{
					let wb = XLSX.utils.book_new()
					XLSX.utils.book_append_sheet(wb, this.ws)
					XLSX.writeFile(wb, this.filename)
					this.exceldata = []
					this.dumpshow = false
					this.dumppage = 1
					this.percentage = 0
				}
			})
		},
		closedialog(){
			this.dumpshow = false
		},
		restore(row){
			this.$confirm('确定恢复数据吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let ids = row.catagory_id ? row.catagory_id : this.ids.join(',')
				this.$axios.post('/admin/Catagory/restore',{catagory_id:ids}).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
						this.index()
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}).catch(() => {})
		},
		selection(selection) {
			this.ids = selection.map(item => item.catagory_id)
			this.single = selection.length != 1
			this.multiple = !selection.length
		},
		handleRowClick(row, rowIndex,event){
			if(event.target.className !== 'el-input__inner'){
				this.$refs.multipleTable.toggleRowSelection(row)
			}
		},
		rowClass ({ row, rowIndex }) {
			for(let i=0;i<this.ids.length;i++) {
				if (row.catagory_id === this.ids[i]) {
					return 'rowLight'
				}
			}
		},
		sortChange(val){
			if(val.order == 'descending'){
				this.order= 'desc'
			}
			if(val.order == 'ascending'){
				this.order= 'asc'
			}
			this.sort = val.prop
			this.index()
		},
		toggleRowExpansion(){
			this.expand = !this.expand
			this.list.forEach(item=>{
				this.$refs.multipleTable.toggleRowExpansion(item,this.expand)
			})
		},
		/** 转换菜单数据结构 */
		normalizer(node) {
			if (node.children && !node.children.length) {
				delete node.children
			}
			return {
				id: node.val,
				label: node.key,
				children: node.children
			}
		},
		toggleList(){
			this.searchData.onlyTrashed = this.hsz
			this.index()
		},
		fn(method){
			this[method](this.ids)
		},
	},
	mounted(){
		this.index()
	},
}
</script>

<style  lang="scss">
@import '@/assets/scss/common.scss'
</style>
